/*
 * Copyright 2009 Kaspar Minosiants http://www.minosiants.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.minosiants.confluence.blog.plugin.impl.tweet2it;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.minosiants.confluence.blog.plugin.tweet2it.ApplicationException;
import com.minosiants.confluence.blog.plugin.tweet2it.IContextUtil;
import com.minosiants.confluence.blog.plugin.tweet2it.IOAuthManager;
import com.minosiants.confluence.blog.plugin.tweet2it.ITwitterClient;
import com.minosiants.confluence.blog.plugin.tweet2it.ITwitterService;
import com.minosiants.confluence.blog.plugin.tweet2it.StatusBean;
import com.minosiants.confluence.blog.plugin.tweet2it.TwitterDirectMessage;
import com.minosiants.confluence.blog.plugin.tweet2it.TwitterStatus;
import com.minosiants.confluence.blog.plugin.tweet2it.TwitterUser;

/**
 *	Project home: http://tweet2it.minosiants.com
 *
 *  @author Kaspar Minosiants
 *
 * 	$Id:$
 */
public class TwitterService implements ITwitterService {
	private Log logger = LogFactory.getLog(getClass());
	private ITwitterClient twitterClient;
	private IOAuthManager oauthManager;
	private IContextUtil contextUtil;
			
	


	public Collection<TwitterStatus> getReplies(Long page){
		return twitterClient.getMentions(page);
	}
	//@CheckPermission
	public Collection<TwitterDirectMessage> getDirect(Long page){
		checkUserPermission();
		return twitterClient.getDirect(page);
	}
	public int getDirectCount(){
		return 0;
	}

	public Collection<TwitterStatus> getFavorites(Long page){
		return twitterClient.getFavorites(page);
	}
	

	public TwitterUser getMyProfileInfo(){
		return twitterClient.getTwitterUser(getTwitterUserId());
	}

	public TwitterUser getUserInfo(String twitterUserId){
		return twitterClient.getTwitterUser(twitterUserId);
	}
	//@CheckPermission	
	public TwitterStatus updateStatus(StatusBean statusBean){
		checkUserPermission();
		if(statusBean.getReplayTo()==null){
			return twitterClient.updateStatus(statusBean.getStatus());
			
		}
		return twitterClient.updateStatus(statusBean.getStatus(),""+statusBean.getReplayTo());
	}

	public Collection<TwitterStatus> getFriendsTimeLine(Long page){
		return twitterClient.getFriendsTimeLine(page);
	}

	public Collection<TwitterStatus> getUserTimeLine(Long page){
		return twitterClient.getUserTimeline(page);
	}

	public Collection<TwitterStatus> getEveryOne(Long page){
		return twitterClient.getPublicTimeline(page);	
	}

	public boolean  addToFavorites(Long id){
		return twitterClient.addToFavorites(id);
	}
	//@CheckPermission
	public boolean  removeFromFavorites(Long id){
		checkUserPermission();
		return twitterClient.removeFromFavorites(id);
	}
	//@CheckPermission
	public boolean  deleteStatus(Long statusId){
		checkUserPermission();
		return twitterClient.deleteStatus(statusId);
	}

	public String  getAuthUrl(){
		return oauthManager.getAuthUrlFor(getTwitterUserId());
	}

	public boolean  storeTokenWithSecret(String pin){
		return oauthManager.storeTokenWithSecretFor(getCurrientUserId(),getTwitterUserId(),pin);					
	}

	public boolean  userExists(){			
		return oauthManager.getUserData(getCurrientUserId())!=null;
	}	

	private String getTwitterUserId(){		
		return contextUtil.getAttribute(String.class, IContextUtil.Constants.TWITTER_USER);
	}
	private String getCurrientUserId(){
		return contextUtil.getCurrientUserId();	
	}
	public void setOauthManager(IOAuthManager oauthManager) {
		this.oauthManager = oauthManager;
	}

	
	public void setTwitterClient(ITwitterClient twitterClient) {
		this.twitterClient = twitterClient;
	}

	public void setContextUtil(IContextUtil contextUtil) {
		this.contextUtil = contextUtil;
	}

	
	public boolean removeDirectMessages(String[] ids) {
		for (String id : ids) {
			twitterClient.deleteDirectMessage(id);
		}
		return true;
	}

	@Override
	public void logout() {
		oauthManager.removeUser(getCurrientUserId());		
	}
	private void checkUserPermission(){
    	logger.info("checkUserPermission");
    	if(!contextUtil.isReadOnly())
    		return;
    	throw new ApplicationException("operation is not allowed");
	}
}	
